import { create } from 'zustand';
interface StoreType {
  timer: number;
  beginTimer: () => void;
}
const useTimerStore = create<StoreType>((set) => ({
  timer: 0,
  // 开始计时器
  beginTimer: () => {
    const time = localStorage.getItem('timer');
    set(() => ({ timer: Number(time) || 60 }));
    const interval = setInterval(() => {
      set((state) => {
        if (state.timer <= 0) {
          clearInterval(interval);
          localStorage.removeItem('timer');
          return { timer: 0 };
        }
        localStorage.setItem('timer', String(state.timer - 1));
        return { timer: state.timer - 1 };
      });
    }, 1000);
  },
}));

export default useTimerStore;
